package com.bytedance.common.plugin.component;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.Handler;
import android.os.UserHandle;

import com.bytedance.common.plugin.framework.PluginManager;
import com.bytedance.common.plugin.component.ComponentHelper.COMPONENT_TYPE;

public class PluginContextImpl extends ContextWrapper {
    private boolean mIsOrigin;

    public PluginContextImpl(Context arg2, boolean arg3) {
        super(arg2);
        this.mIsOrigin = false;
        this.mIsOrigin = arg3;
    }

    public boolean bindService(Intent arg4, ServiceConnection arg5, int arg6) {
        Intent[] v0 = ComponentHelper.tryConvertPlugin2Proxy(((Context) this), COMPONENT_TYPE.SERVICE, new Intent[]{arg4});
        if (v0 != null && v0.length > 0) {
            arg4 = v0[0];
        }

        return super.bindService(arg4, arg5, arg6);
    }

    public AssetManager getAssets() {
        AssetManager v0_1;
        try {
            if (this.mIsOrigin) {
                v0_1 = PluginManager.getPluginManager().e;
                return v0_1;
            }

            v0_1 = PluginManager.getPluginManager().mAssetManager;
        } catch (Throwable v0) {
            v0_1 = super.getAssets();
        }

        return v0_1;
    }

    public Resources getResources() {
        Resources v0_1;
        try {
            if (this.mIsOrigin) {
                v0_1 = PluginManager.getPluginManager().d;
                return v0_1;
            }

            v0_1 = PluginManager.getPluginManager().mResources;
        } catch (Throwable v0) {
            v0_1 = super.getResources();
        }

        return v0_1;
    }

    public void sendBroadcast(Intent arg4) {
        Intent[] v0 = ComponentHelper.tryConvertPlugin2Proxy(((Context) this), COMPONENT_TYPE.RECEIVER, new Intent[]{arg4});
        if (v0 != null && v0.length > 0) {
            arg4 = v0[0];
        }

        super.sendBroadcast(arg4);
    }

    public void sendBroadcast(Intent arg4, String arg5) {
        Intent[] v0 = ComponentHelper.tryConvertPlugin2Proxy(((Context) this), COMPONENT_TYPE.RECEIVER, new Intent[]{arg4});
        if (v0 != null && v0.length > 0) {
            arg4 = v0[0];
        }

        super.sendBroadcast(arg4, arg5);
    }

    public void sendOrderedBroadcast(Intent arg4, String arg5) {
        Intent[] v0 = ComponentHelper.tryConvertPlugin2Proxy(((Context) this), COMPONENT_TYPE.RECEIVER, new Intent[]{arg4});
        if (v0 != null && v0.length > 0) {
            arg4 = v0[0];
        }

        super.sendOrderedBroadcast(arg4, arg5);
    }

    public void sendOrderedBroadcast(Intent arg9, String arg10, BroadcastReceiver arg11, Handler arg12, int arg13, String arg14, Bundle arg15) {
        Intent[] v0 = ComponentHelper.tryConvertPlugin2Proxy(((Context) this), COMPONENT_TYPE.RECEIVER, new Intent[]{arg9});
        Intent v1 = v0 == null || v0.length <= 0 ? arg9 : v0[0];
        super.sendOrderedBroadcast(v1, arg10, arg11, arg12, arg13, arg14, arg15);
    }

    @SuppressLint("MissingPermission")
    @TargetApi(value = 17)
    public void sendOrderedBroadcastAsUser(Intent arg10, UserHandle arg11, String arg12, BroadcastReceiver arg13, Handler arg14, int arg15, String arg16, Bundle arg17) {
        Intent[] v0 = ComponentHelper.tryConvertPlugin2Proxy(((Context) this), COMPONENT_TYPE.RECEIVER, new Intent[]{arg10});
        Intent v1 = v0 == null || v0.length <= 0 ? arg10 : v0[0];
        super.sendOrderedBroadcastAsUser(v1, arg11, arg12, arg13, arg14, arg15, arg16, arg17);
    }

    @SuppressLint("MissingPermission")
    @TargetApi(value = 17)
    public void sendStickyBroadcast(Intent arg4) {
        Intent[] v0 = ComponentHelper.tryConvertPlugin2Proxy(((Context) this), COMPONENT_TYPE.RECEIVER, new Intent[]{arg4});
        if (v0 != null && v0.length > 0) {
            arg4 = v0[0];
        }

        super.sendStickyBroadcast(arg4);
    }

    @SuppressLint("MissingPermission")
    @TargetApi(value = 17)
    public void sendStickyBroadcastAsUser(Intent arg4, UserHandle arg5) {
        Intent[] v0 = ComponentHelper.tryConvertPlugin2Proxy(((Context) this), COMPONENT_TYPE.RECEIVER, new Intent[]{arg4});
        if (v0 != null && v0.length > 0) {
            arg4 = v0[0];
        }

        super.sendStickyBroadcastAsUser(arg4, arg5);
    }

    @SuppressLint("MissingPermission")
    public void sendStickyOrderedBroadcast(Intent arg8, BroadcastReceiver arg9, Handler arg10, int arg11, String arg12, Bundle arg13) {
        Intent[] v0 = ComponentHelper.tryConvertPlugin2Proxy(((Context) this), COMPONENT_TYPE.RECEIVER, new Intent[]{arg8});
        Intent v1 = v0 == null || v0.length <= 0 ? arg8 : v0[0];
        super.sendStickyOrderedBroadcast(v1, arg9, arg10, arg11, arg12, arg13);
    }

    @SuppressLint("MissingPermission")
    @TargetApi(value = 17)
    public void sendStickyOrderedBroadcastAsUser(Intent arg9, UserHandle arg10, BroadcastReceiver arg11, Handler arg12, int arg13, String arg14, Bundle arg15) {
        Intent[] v0 = ComponentHelper.tryConvertPlugin2Proxy(((Context) this), COMPONENT_TYPE.RECEIVER, new Intent[]{arg9});
        Intent v1 = v0 == null || v0.length <= 0 ? arg9 : v0[0];
        super.sendStickyOrderedBroadcastAsUser(v1, arg10, arg11, arg12, arg13, arg14, arg15);
    }

    @TargetApi(value = 11)
    public void startActivities(Intent[] arg2) {
        super.startActivities(ComponentHelper.tryConvertPlugin2Proxy(((Context) this), COMPONENT_TYPE.ACTIVITY, arg2));
    }

    @TargetApi(value = 16)
    public void startActivities(Intent[] arg2, Bundle arg3) {
        super.startActivities(ComponentHelper.tryConvertPlugin2Proxy(((Context) this), COMPONENT_TYPE.ACTIVITY, arg2), arg3);
    }

    public void startActivity(Intent arg4) {
        Intent[] v0 = ComponentHelper.tryConvertPlugin2Proxy(((Context) this), COMPONENT_TYPE.ACTIVITY, new Intent[]{arg4});
        if (v0 != null && v0.length > 0) {
            arg4 = v0[0];
        }

        super.startActivity(arg4);
    }

    @TargetApi(value = 16)
    public void startActivity(Intent arg4, Bundle arg5) {
        Intent[] v0 = ComponentHelper.tryConvertPlugin2Proxy(((Context) this), COMPONENT_TYPE.ACTIVITY, new Intent[]{arg4});
        if (v0 != null && v0.length > 0) {
            arg4 = v0[0];
        }

        super.startActivity(arg4, arg5);
    }

    public ComponentName startService(Intent arg4) {
        Intent[] v0 = ComponentHelper.tryConvertPlugin2Proxy(((Context) this), COMPONENT_TYPE.SERVICE, new Intent[]{arg4});
        if (v0 != null && v0.length > 0) {
            arg4 = v0[0];
        }

        return super.startService(arg4);
    }

    public boolean stopService(Intent arg4) {
        Intent[] v0 = ComponentHelper.tryConvertPlugin2Proxy(((Context) this), COMPONENT_TYPE.SERVICE, new Intent[]{arg4});
        if (v0 != null && v0.length > 0) {
            arg4 = v0[0];
        }

        return super.stopService(arg4);
    }
}

